package com.minyifang.wcspf.work.ui.goods.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.minyifang.wcspf.R;
import com.minyifang.wcspf.common.utils.ArrayUtil;
import com.minyifang.wcspf.work.ui.goods.activities.BrowsePicActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PicturesAdapter extends RecyclerView.Adapter<PicturesAdapter.Holder> {

    public enum Type {
        WRAP, GRID
    }

    private RequestOptions mRequestOptions = new RequestOptions().placeholder(R.mipmap.img_place_holder);

    private Context mContext;
    private Type mType;
    private List<String> mPicList;

    public PicturesAdapter(Context context, Type type, List<String> picList) {
        mContext = context;
        mType = type;
        mPicList = picList;
    }

    public PicturesAdapter(Context context, Type type, String[] picArray) {
        mContext = context;
        mType = type;
        mPicList = Arrays.asList(picArray);
    }

    @SuppressWarnings("unchecked")
    public <T> PicturesAdapter(Context context, Type type, List<T> list, ArrayUtil.StringConverter converter) {
        mContext = context;
        mType = type;
        mPicList = ArrayUtil.listConvertToStringList(list, converter);
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Holder(LayoutInflater.from(mContext).inflate(mType == Type.WRAP ? R.layout.item_goods_picture_wrap : R.layout.item_goods_picture, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final Holder holder, int position) {
        Glide.with(mContext).load(mPicList.get(position)).apply(mRequestOptions).into(holder.ivPicture);
//                holder.ivPicture.setImageDrawable(new ColorDrawable(Color.rgb(new Random().nextInt(255), new Random().nextInt(255), new Random().nextInt(255))));
        if (mType == Type.GRID) {
            holder.itemView.setOnClickListener(v -> BrowsePicActivity.start(mContext, mPicList, holder.getAdapterPosition()));
        }
    }

    @Override
    public int getItemCount() {
        return mPicList.size();
    }

    public class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_picture)
        ImageView ivPicture;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}